package com.simple.util.math.function;

import java.math.BigDecimal;

/**
 * @program: simple_tools
 * @description: 数学(反比例函数)
 * @author: Mr.chen
 * @create: 2019-10-24 22:10
 **/
public class InverseFunction {

    // y = k/x
    //斜率
    private double k = 1;
    public static InverseFunction instance;
    private InverseFunction(){};
    static{
        if(instance == null){
            synchronized (InverseFunction.class){
                if(instance == null){
                    instance = new InverseFunction();
                }
            }
        }
    }

    /**
     * 创建反比例函数
     * @param k
     */
    public static void init(double k){
        if(k == 0){
            throw new RuntimeException("param k is not be zero");
        }
        instance.setK(k);
    }

    /**
     * 使用一个点初始化反比例函数
     * @param point
     */
    public static void init(Point point){
        if(point == null){
            throw new RuntimeException("point k is not be null");
        }
        double x = point.getX();
        double y = point.getY();
        double k = new BigDecimal(x / y).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        instance.setK(k);
    }

    /**
     * 判断点point是否在线上
     * @param point
     * @return
     */
    public static boolean isOnline(Point point){
        if(point == null){
            throw new RuntimeException("point is not be null !");
        }
        double x = point.getX();
        double y = point.getY();
        return instance.getK() == x / y;
    }

    //点
    public static class Point{

        // x坐标
        private double x;
        // y坐标
        private double y;
        public Point(double x,double y){
            this.x = x;
            this.y = y;
        }

        public double getX() {
            return x;
        }

        public void setX(double x) {
            this.x = x;
        }

        public double getY() {
            return y;
        }

        public void setY(double y) {
            this.y = y;
        }
    }

    @Override
    public String toString() {
        return "InverseFunction{" +
                "k=" + k +
                '}';
    }

    public double getK() {
        return k;
    }

    public void setK(double k) {
        this.k = k;
    }
}
